<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Zprovice extends Common
{

	//列表
    public function index()
    {
        $title = $this->request->get('title');
        $head = $this->request->get('head');
        $admin = $this->request->get('admin');
        $zoning = $this->request->get('zoning');
        $this->assign(array('title'=>$title));
        $this->assign(array('head'=>$head));
        $this->assign(array('admin'=>$admin));
        $this->assign(array('zoning'=>$zoning));
        $where = [];
        $where['isdel'] = 0;
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        if($head)
            $where['head'] = array('exp','in(select id from employee where em_name like "%'.$head.'%")');
        if($zoning)
            $where['zoning'] = $zoning;
        if($admin){
            //根据模糊查询规则把admin对应的id全部查询出来
            $ids = Db::query("select id from employee where em_name like '%{$admin}%'");
            $str = [];
            foreach($ids as $k=>$v){
                if($k==0)
                    $str[] = " like '%\"$v[id]\"%' ";
                else
                    $str[] = " admin like '%\"$v[id]\"%' ";
            }
            $str = implode('or',$str);

            $where['admin'] = array('exp',$str);
        }

        $count = Db::table('cg_market_provice')->where($where)->count();
        $data = Db::table('cg_market_provice')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_market_provice')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['head'] = Db::table('employee')->where(array('id'=>$v['head']))->value('em_name');
            $data[$k]['zoning'] = Db::table('cg_market_zoning')->where(array('id'=>$v['zoning']))->value('title');
            $temp = unserialize($v['admin']);
            $temp = implode(',',$temp);
            $temp = Db::query("select em_name from employee where id in(".$temp.")");
//            echo Db::table('employee')->getLastSql();
//            var_dump($temp);
            $temp2 = [];
            foreach($temp as $v1){
                $temp2[] = $v1['em_name'];
            }
            $temp2 = implode(',',$temp2);
            $data[$k]['admin'] = $temp2;
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]["provice"]=db("cg_market_city")->where("provice={$v["id"]}")->column("title");
            if($data[$k]["provice"])
                $data[$k]["provice"]=implode(',',$data[$k]["provice"]);
            else
                $data[$k]["provice"]="";
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
		$this->assign('count',$count);
        //查询分区
        $fenqu = Db::table('cg_market_zoning')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);
        return $this->fetch('index');
    }
    public function add(){

        $validate = new Validate([
            'title|省份名称'  => 'require|max:100|unique:cg_market_provice',
            'head|负责人'  => 'require',
            'admin|管理人员'  => 'require',
            'zoning|分区'  => 'require',
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['head'] = $this->request->post('head');
        $data['admin'] = $this->request->post('admin');
        $data['zoning'] = $this->request->post('zoning');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['admin'] = explode(',',$data['admin']);
        $data['admin'] = serialize($data['admin']);
        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_market_provice')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    //表单增加
    public function addview(){
        //查询分区
        $fenqu = Db::table('cg_market_zoning')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);
        return $this->fetch();
    }
    //批量设置分区
    public function setzone(){

        if($_POST){
            $provices = $this->request->post('provices');
            $zoning = $this->request->post('zoning');
            $provices = str_replace('-',',',$provices);
            $bool = Db::execute("update cg_market_provice set zoning='".$zoning."' where id in(".$provices.")");
            if(false!==$bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"修改成功"
                ]);
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"修改失败"
                ]);
            }
            die;
        }

        $provices = $this->request->get('provices');
        $this->assign('provices',$provices);
        //查询分区
        $fenqu = Db::table('cg_market_zoning')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);
        return $this->fetch();
    }
    public function updateview(){
        $id = $this->request->get('id');
        $data = Db::table('cg_market_provice')->where(array('id'=>$id))->find();
        $data['em_name'] = Db::table('employee')->where(array('id'=>$data['head']))->value('em_name');

        $temp = unserialize($data['admin']);
        $temp = implode(',',$temp);
        $temp = Db::query("select em_name,id from employee where id in(".$temp.")");

        $data['admins'] = $temp;

        $this->assign('data',$data);

        //查询分区
        $fenqu = Db::table('cg_market_zoning')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);
        return $this->fetch();
    }

    //查询部门负责人
    public function selecthead(){
        return $this->fetch();
    }
    //查询部门负责人
    public function selectadmin(){
        return $this->fetch();
    }
    
    public function provicejson(){
        //查询所有省份
        $data = Db::table('employee')->field('em_name,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function update(){

        $id = $this->request->post('id');
        $data = [];
        $data['id'] = $id;
        $data['title'] = $this->request->post('title');
        $data['head'] = $this->request->post('head');
        $data['admin'] = $this->request->post('admin');
        $data['zoning'] = $this->request->post('zoning');
        $validate = new Validate([
            'title|省份名称'  => 'require|max:100|unique:cg_market_provice',
            'id' => 'require|number',
            'head|负责人'  => 'require',
            'admin|管理人员'  => 'require',
            'zoning|分区'  => 'require',
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['admin'] = explode(',',$data['admin']);
        $data['admin'] = serialize($data['admin']);
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_market_provice')->where(array('id'=>$id))->update($data);
//        echo Db::table('cg_market_provice')->getLastSql();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

//    删除
    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['isdel'] = 1;
        $bool = Db::table('cg_market_provice')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }
    public function qiyong(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['isdel'] = 0;
        $bool = Db::table('cg_market_provice')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"启用成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"启用失败"
            ]);die;
        }
    }

//    回收站
    public function isdel(){
        $title = $this->request->get('title');
        $head = $this->request->get('head');
        $admin = $this->request->get('admin');
        $zoning = $this->request->get('zoning');
        $this->assign(array('title'=>$title));
        $this->assign(array('head'=>$head));
        $this->assign(array('admin'=>$admin));
        $this->assign(array('zoning'=>$zoning));
        $where = [];
        $where['isdel'] = 1;
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        if($head)
            $where['head'] = array('exp','in(select id from employee where em_name like "%'.$head.'%")');
        if($zoning)
            $where['zoning'] = $zoning;
        if($admin){
            //根据模糊查询规则把admin对应的id全部查询出来
            $ids = Db::query("select id from employee where em_name like '%{$admin}%'");
            $str = [];
            foreach($ids as $k=>$v){
                if($k==0)
                    $str[] = " like '%\"$v[id]\"%' ";
                else
                    $str[] = " admin like '%\"$v[id]\"%' ";
            }
            $str = implode('or',$str);

            $where['admin'] = array('exp',$str);
        }

        $count = Db::table('cg_market_provice')->where($where)->count();
        $data = Db::table('cg_market_provice')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_market_provice')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['head'] = Db::table('employee')->where(array('id'=>$v['head']))->value('em_name');
            $data[$k]['zoning'] = Db::table('cg_market_zoning')->where(array('id'=>$v['zoning']))->value('title');
            $temp = unserialize($v['admin']);
            $temp = implode(',',$temp);
            $temp = Db::query("select em_name from employee where id in(".$temp.")");
            $temp2 = [];
            foreach($temp as $v1){
                $temp2[] = $v1['em_name'];
            }
            $temp2 = implode(',',$temp2);
            $data[$k]['admin'] = $temp2;
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        //查询分区
        $fenqu = Db::table('cg_market_zoning')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);

        return $this->fetch();
    }
}
